package com.atguigu.zhxy.service.impl;

import com.atguigu.zhxy.mapper.TeacherMapper;
import com.atguigu.zhxy.pojo.LoginForm;
import com.atguigu.zhxy.pojo.Teacher;
import com.atguigu.zhxy.service.TeacherService;
import com.atguigu.zhxy.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public Teacher login(LoginForm loginForm) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("name",loginForm.getUsername()) ;
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword())) ;
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Teacher getTeacherById(Long userId) {
        return baseMapper.selectById(userId);
    }

    @Override
    public IPage<Teacher> getGradesByPN(Page<Teacher> page, Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper  = new QueryWrapper<>() ;
        String clazzName = teacher.getClazzName();
        String name = teacher.getName();
        if(!StringUtils.isEmpty(clazzName)){
            queryWrapper.like("clazz_name",clazzName);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByDesc("id") ;
        Page<Teacher> studentPage = baseMapper.selectPage(page, queryWrapper);
        return studentPage;
    }
}
